package pku.tcs.wsc.solver;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
import java.util.Map.Entry;

import org.jdom.JDOMException;

import pku.tcs.wsc.compositionSystem.CompositionAlgorithm;
import pku.tcs.wsc.parser.OWLParser;
import pku.tcs.wsc.parser.WSDLParser;
import pku.tcs.wsc.parser.WSLAParser;
import pku.tcs.wsc.service.Service;
import pku.tcs.wsc.service.ServicePool;

/**
 * This is the solver class
 * 
 * @author Lilei
 * 
 */

public class Solver {

	private static String wsdlFilePath = null;
	private static String wslaFilePath = null;
	private static String owlFilePath = null;
	private static String challengeFilePath = null;
	private static String parseResultPath = null;

	private static ServicePool pool = new ServicePool();

	private static WSDLParser wsdlParser;
	private static WSLAParser wslaParser;
	private static OWLParser owlParser;

	public static void setWSDLFilePath(String path) {
		wsdlFilePath = path;
	}

	public static void setWSLAFilePath(String path) {
		wslaFilePath = path;
	}

	public static void setOWLFilePath(String path) {
		owlFilePath = path;
	}

	public static void setChallengeFilePath(String path) {
		challengeFilePath = path;
	}

	public static void setParserResultPath(String path) {
		parseResultPath = path;
	}

	@SuppressWarnings("unchecked")
	public static ArrayList<Service> solve() throws FileNotFoundException, JDOMException, IOException{
//		if(wsdlFilePath==null)
//			return ;
//		if(wslaFilePath==null)
//			return ;
		
		owlParser = new OWLParser(owlFilePath);
		wsdlParser = new WSDLParser(wsdlFilePath,pool);
		wsdlParser.parse();
		wslaParser = new WSLAParser(wslaFilePath,pool);
		wslaParser.parse();
		
		//now all the services is added into service pool
		Vector<Service> serviceVector=new Vector<Service>();
		Iterator iter = pool.pool.entrySet().iterator();
		while(iter.hasNext()){
			Entry<String,Service> entry=(Entry) iter.next();
			serviceVector.add(entry.getValue());
		}
		
		
		if(parseResultPath!=null){
			try {
				pool.writeParseResult(parseResultPath);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		Service requiredService =wsdlParser.parseChallengeFile(challengeFilePath);
		return CompositionAlgorithm.startQuery(requiredService, serviceVector,owlParser);
	}

	public static void main(String[] args) throws FileNotFoundException,
			JDOMException, IOException {
//		String owlFile = "data/testSet_V1/test_V1.owl";
//		String challengeFile = "data/testSet_V1/testProblem_V1.wsdl";
//		String wsdlFile = "data/testSet_V1/test_V1.wsdl";
//		String wslaFile = "data/testSet_V1/test_V1.wsla";
//		String result = "./data/ParseResult.txt";
		String foldername="testSet_V2";
		String owlFile = "data/"+foldername+"/Taxonomy.owl";
		String challengeFile = "data/"+foldername+"/Challenge.wsdl";
		String wsdlFile = "data/"+foldername+"/Services.wsdl";
		String wslaFile = "data/"+foldername+"/Servicelevelagreements.wsla";
		String result = "./data/"+foldername+"/ParseResult.txt";

		Solver.setParserResultPath(result);
		Solver.setWSDLFilePath(wsdlFile);
		Solver.setOWLFilePath(owlFile);
		Solver.setWSLAFilePath(wslaFile);
		Solver.setChallengeFilePath(challengeFile);

		ArrayList<Service> v=Solver.solve();
		for(Service s:v)
			System.out.println(s.id+" "+s.responseTime+" "+s.throughput);
	}
}
